﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace Test
{
    public partial class Form1 : XtraForm
    {
        public Form1()
        {
            InitializeComponent();
        }
        public void p()
        {
            //this.timer1.Enabled = true;
            this.progressBarControl1.Position = 0;Thread t = new Thread((obj) => {
                if (this.InvokeRequired)
                {
                    this.Invoke(new MethodInvoker(this.p));
                    return;
                }
                
                while (!Flag)
                {
                    Thread.Sleep(50);
                    //this.marqueeProgressBarControl1.Invoke(new DoRefresh(this.marqueeProgressBarControl1.Refresh));
                    this.marqueeProgressBarControl1.Update();
                    //this.DoProgressBar();

                }
            });
            t.Start();
            
            Thread.Sleep(5000);Form1.Flag = true;//this.marqueeProgressBarControl1.EditValue
        }

        public static bool Flag = false;
        private void simpleButton1_Click(object sender, EventArgs e)
        {
            //p();
            this.progressBarControl1.Properties.Minimum = 0;
            this.progressBarControl1.Properties.Maximum = 100;
            this.progressBarControl1.Position = 0;
            for (int i = 0; i < 100; i++)
            {
                this.progressBarControl1.Increment(1);
                Thread.Sleep(50);
                this.progressBarControl1.Update();
            }
            Thread.Sleep(2000);
            this.progressBarControl1.Properties.Minimum = 0;
            this.progressBarControl1.Properties.Maximum = 100;
            this.progressBarControl1.Position = 0;
            for (int i = 0; i < 100; i++)
            {
                this.progressBarControl1.Increment(1);
                Thread.Sleep(50);
                this.progressBarControl1.Update();
            }

            //this.marqueeProgressBarControl1.
            //this.progressBar1.Minimum = 0;
            //this.progressBar1.Maximum = 100;
            //this.progressBar1.Value = 0;
            //for (int i = 0; i < 100; i++)
            //{
            //    this.progressBar1.Value++;
            //    Thread.Sleep(50);
            //}

        }

        public delegate void DoRefresh();
        public void DoProgressBar()
        {
            this.progressBar1.Value++; if (this.progressBarControl1.Position == 10)
                this.progressBarControl1.Position = 0;
            //this.progressBarControl1.Invalidate();//this.progressBarControl1.Text = "abc";
            this.marqueeProgressBarControl1.Refresh();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            //this.marqueeProgressBarControl1.Invalidate();
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            NewMethod();
        }
  
        private void NewMethod()
        {
            //if (this.InvokeRequired) {
            //    this.Invoke(new MethodInvoker(this.NewMethod));
            //    return;
            //}
            Token tk = NewDialogProcess();
            Thread.Sleep(5000);
            //MessageBox.Show("End");
            tk.Flag = true;
        }
  
        private Token NewDialogProcess()
        {
            //if (this.InvokeRequired) {
            //    this.Invoke(new MethodInvoker(this.NewMethod));
            //    return;
            //}
            Token tk = new Token();
            Thread t = new Thread((obj) =>
            {
                Form2 f2 = new Form2((Token)obj);
                f2.ShowDialog();
                
            });
              
            t.Start(tk);
            return tk;
        }

        private void f(Form2 dialog) {
            dialog.ShowDialog(this);
        }

        public delegate void dgShowDialog(Form2 f);
    }
}
